<?php
/**
 * 获取当前登录的用户的session.
 * ps:APP中通过token去获取缓存中的用户信息.
 *
 * @param string $key
 *
 * @return mixed
 */
function xcxapi_get_current_user_session($key = '')
{
    $request = request();
    if (!$token = $request->header('X-Token')) {
        return [];
    }
    if (!$type = $request->header('X-Type')) {
        return [];
    }
    $sessionData = cache('EDU_TOKEN:'.$type.':'.$token);
    if ($sessionData) {
        if ($key) {
            return isset($sessionData[$key]) ? $sessionData[$key] : [];
        }

        return $sessionData;
    }

    return [];
}

/**
 * 获取当前登录用户的组织的等级.
 *
 * @return int
 */
function xcxapi_get_current_user_organ_level()
{
    return (int) xcxapi_get_current_user_session('level_id');
}

/**
 * 生成token.
 *
 * @return string
 */
function xcxapi_generate_token()
{
    // 请求ip
    $ip = $_SERVER['REMOTE_ADDR'];
    // 时间戳
    $timestamp = $_SERVER['REQUEST_TIME_FLOAT'];
    // salt随机盐
    $salt = uniqid();

    return md5($ip.$timestamp.$salt);
}

/**
 * 获取用户数据使用权限.
 *
 * @param $where
 * @param string $type 多个参数逗号隔开
 *
 * @throws ErrorException
 *
 * @return array
 */
function xcxapi_get_user_data_privilege(&$where, $type = 'B1')
{
    $_authType = ['B1', 'B2', 'B3', 'B4', 'B5', 'C1', 'C2'];

    $type = strpos($type, ',') ? explode(',', $type) : [$type];
    $level = xcxapi_get_current_user_organ_level();
    $maxLevel = xcxapi_get_business_organ_max_level();

    if (!is_array($where)) {
        throw new \ErrorException('参数错误');
    }

    $organIds = xcxapi_get_current_user_session('data_use_privilege');
    foreach ($type as $t) {
        if (!in_array($t, $_authType, true)) {
            throw new \ErrorException('参数错误');
        }
        $_type = mb_substr($t, 0, 1);
        // 当前登陆用户为一级组织且所属商家组织结构为三层时使用C规则.
        if ('C' === $_type && 1 === $level && 3 === $maxLevel) {
            if (isset($organIds[$t])) {
                $where['organ_id'] = ('C2' === $t) ? $organIds[$t] : ['in', $organIds[$t]];
            }

            continue;
        }

        if ('B' === $_type && !isset($where['organ_id'])) {
            if (isset($organIds[$t])) {
                $where['organ_id'] = ('B5' === $t) ? $organIds[$t] : ['in', $organIds[$t]];
            }

            continue;
        }
    }

    return $where;
}

/**
 * 获取当前登录的管理员ID
 * User: wufeng
 * Date: 2019/5/24.
 *
 * @return int
 */
function xcxapi_get_current_user_id()
{
    return (int) xcxapi_get_current_user_session('user_id');
}

/**
 * 获取当前登录的管理员姓名
 * User: wufeng
 * Date: 2019/5/24.
 *
 * @return mixed
 */
function xcxapi_get_current_name()
{
    return xcxapi_get_current_user_session('name');
}

/**
 * 获取当前登录的管理员所在校区ID
 * User: wufeng
 * Date: 2019/5/24.
 *
 * @return int
 */
function xcxapi_get_current_campus_id()
{
    return xcxapi_get_current_user_session('campus_id');
}

/**
 * 获取当前登录的管理员所在集团ID
 * User: wufeng
 * Date: 2019/5/24.
 *
 * @return int
 */
function xcxapi_get_current_company_id()
{
    return (int) xcxapi_get_current_user_session('company_id');
}

/**
 * 获取当前登录的管理员所属权限类型
 * User: wufeng
 * Date: 2019/5/24.
 *
 * @return int
 */
function xcxapi_get_current_wtype()
{
    return (int) xcxapi_get_current_user_session('wtype');
}

/**
 * 获取当前登录的管理员所属用户组属性
 * User: wufeng
 * Date: 2020/04/16
 *
 * @return int
 */
function xcxapi_get_current_group_type()
{
    return (int) xcxapi_get_current_user_session('group_type');
}

/**
 * 获取当前登录的管理员头像
 * User: wufeng
 * Date: 2019/6/4.
 *
 * @return mixed
 */
function xcxapi_get_current_img()
{
    return xcxapi_get_current_user_session('img');
}

/**
 * @param string $url get请求地址
 * @param int $httpCode 返回状态码
 * @return mixed
 */
function curl_get($url, &$httpCode = 0)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	//不做证书校验,部署在linux环境下请改为true
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
	$file_contents = curl_exec($ch);
	$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);
	return $file_contents;
}

/**
 * 生成订单号
 * User: wufeng
 * Date: 2020/04/01
 *
 * @param string $type
 * @param string $num
 * @return string
 */
function orderNumber($type = '',$num = ''){
	$number = '';
	$year = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
	$number .= $year[intval(date('Y')) - 2018] . date('mdHis').substr(microtime(), 2, 3) . sprintf('%02d', rand(0, 99));
	return $number;
}
